package com.whfc.ms.api.emp.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.ImageUtil;
import com.whfc.common.util.RandomUtil;
import com.whfc.emp.dto.AppEmpDTO;
import com.whfc.emp.dto.AppTrainDTO;
import com.whfc.emp.dto.AppTrainEmpDTO;
import com.whfc.emp.dto.TrainEmpDTO;
import com.whfc.emp.param.*;
import com.whfc.emp.service.AppTrainService;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.ms.api.emp.service.MsTrainService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * @ClasssName MsTrainServiceImpl
 * @Description 人员管理-培训
 * @Author hw
 * @Date 2020/11/26 17:24
 * @Version 1.0
 */
@Service
public class MsTrainServiceImpl implements MsTrainService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = AppTrainService.class, version = "1.0.0", timeout = 5000)
    private AppTrainService appTrainService;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private FileHandler fileHandler;

    @Override
    public PageData<AppTrainDTO> list(AppTrainListParam request) {
        return appTrainService.list(request);
    }

    @Override
    public void add(AppTrainAddParam request) {
        appTrainService.add(request);
    }

    @Override
    public void edit(AppTrainEditParam request) {
        appTrainService.edit(request);
    }

    @Override
    public void del(Integer trainId) {
        appTrainService.del(trainId);
    }

    @Override
    public void complete(Integer trainId) {
        appTrainService.complete(trainId);
    }

    @Override
    public PageData<AppTrainEmpDTO> trainEmpList(AppTrainEmpListParam request) {
        //查找培训的总体信息
        return appTrainService.trainEmpList(request);
    }

    @Override
    public void trainEmpAdd(AppTrainEmpAddParam request) {
        appTrainService.trainEmpAdd(request);
    }

    @Override
    public void trainEmpDel(AppTrainEmpDelParam request) {
        appTrainService.trainEmpDel(request);
    }

    @Override
    public void score(AppTrainScoreParam request) {
        appTrainService.score(request);
    }

    @Override
    public PageData<AppEmpDTO> getEmpList(Integer pageNum, Integer pageSize, Integer trainId, String keyword, Integer groupId) {
        PageData<AppEmpDTO> pageData = appTrainService.getEmpList(pageNum, pageSize, trainId, keyword, groupId);
        return pageData;
    }

    @Override
    public PageData<AppTrainEmpDTO> list(Integer empId, Integer pageNum, Integer pageSize) {
        return appTrainService.list(empId, pageNum, pageSize);
    }

    @Override
    public OssPathDTO uploadImg(MultipartFile file) {
        String originalFileName = file.getOriginalFilename();
        String suffix = "png";
        if (StringUtils.isNotBlank(originalFileName)) {
            suffix = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
        }
        String fileUrl = "";
        try {
            String path = filePathConfig.getFilePath("emp/train/attachment", RandomUtil.getRandomFileName(), suffix);
            InputStream inputStream = file.getInputStream();
            fileUrl = fileHandler.upload(path, inputStream);
        } catch (Exception e) {
            logger.warn("上传培训附件失败.");
        }
        return new OssPathDTO(fileUrl);
    }

    @Override
    public void saveTrainEmpImg(Integer trainEmpId, List<String> imgUrlList) {
        appTrainService.saveTrainEmpImg(trainEmpId,imgUrlList);
    }

    @Override
    public void uploadSignImg(String signImgBase64Str, Integer trainEmpId) {
        if (StringUtils.isBlank(signImgBase64Str)) {
            return;
        }
        TrainEmpDTO trainEmpInfo = appTrainService.getTrainEmpInfo(trainEmpId);
        if (trainEmpInfo == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "培训人员不存在");
        }
        //验证文件是否存在
        //上传文件
        String fileUrl = "";
        try {
            String path = filePathConfig.getFilePath("emp/train/sign", RandomUtil.getRandomFileName(), "png");
            byte[] imageByte = ImageUtil.base64ToImage(signImgBase64Str);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(imageByte);
            fileUrl = fileHandler.upload(path, inputStream);
        } catch (Exception e) {
            logger.warn("上传培训附件失败.");
        }
        if (StringUtils.isNotBlank(fileUrl)) {
            //保存图片到记录中
            appTrainService.saveSignImg(trainEmpId, fileUrl);
        }
    }
}
